<?php

namespace App\Http\Controllers\Api\Repositories;


use App\Dengje\Utils\Settings;
use App\Models\WxUser as Model;
use App\Models\WxUserFollow;
use App\Models\WxUserTemp;


class WxUserRepositores
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    /**
     * @return mixed
     */
    public function info($uid)
    {
        return (new $this->eloquentClass())->where('id', $uid)->first();
    }

    /**
     * 更新
     * @param $uid
     * @param $data
     * @return mixed
     */
    public static function update($uid, $data)
    {
        // 判断是否需要审核用户的资料
        if (Settings::get('is_audit_user_info') == 1) {
            $model = new Model();
            $user = $model->where('id', $uid)->first();
            $user->user_avatar = $data['user_avatar'];
            $user->user_name = $data['user_name'];
            $user->user_introduce = $data['user_introduce'];
            $user->user_birthday = $data['user_birthday'];
            $user->user_background_maps = $data['user_background_maps'];
            return $user->save();
        } else {
            $UserTempModel = new WxUserTemp();
            $UserTempModel->user_id = $uid;
            $UserTempModel->user_avatar = $data['user_avatar'];
            $UserTempModel->user_name = $data['user_name'];
            $UserTempModel->user_introduce = $data['user_introduce'];
            $UserTempModel->user_birthday = $data['user_birthday'];
            $UserTempModel->user_background_maps = $data['user_background_maps'];
            return $UserTempModel->save();
        }


    }

    /**
     * @param $keyword
     * @param int $uid
     * @param int $limit
     */
    public static function searchUser($keyword, $uid = 0, $limit = 10)
    {
        WxSearchRepositores::record($uid, $keyword);
        $data = (new Model())->where(function ($q) use ($keyword) {
//          $keyword = base64_encode($keyword);
            $q->where('user_name', 'like', '%' . $keyword . '%');
        })
            ->simplePaginate($limit, ['id', 'weixin_name', 'user_avatar', 'user_introduce']);
        $data->map(function ($v) use ($uid) {
            $v->is_follow_user = (new WxUserFollow())->isFollowUser($uid, $v->id);
        });
        return $data;
    }

    /**
     * 订单状态
     * @param $type
     */
    public static function orderType($type)
    {
        $data = [];
        switch ($type) {
            case 1:
                $data['title'] = '会员订单';
                $data['details'] = '开通轻航大会员';
                break;
            case 2:
                $data['title'] = '打赏订单';
                $data['details'] = '打赏了一篇你感兴趣的动态';
                break;
        }
        return $data;
    }

    /**
     * 提现记录状态
     * @param $type
     */
    public static function withdrawalType($type)
    {
        $title = '';
        switch ($type) {
            case 0:
                $title = '受理中';
                break;
            case 1:
                $title = '已到账';
                break;
            case 2:
                $title = '提现异常，请联系客服处理';
                break;
        }
        return $title;
    }

}
